package com.itheima.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.service_20211015_114634.BrandService;
import com.itheima.service_20211015_114634.Impl.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.descriptor.JspConfigDescriptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/brandServlet/*")
public class BrandServlet extends BaseServlet{
    private BrandService brandService = new BrandServiceImpl();
    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //  1.调用service方法
        List<Brand> brands = brandService.selectAll();
        //  2.将java对象转换成json数据  序列化
        String jsonString = JSON.toJSONString(brands);
        //  3.响应json数据到页面
        response.setContentType("text/json;charset=utf-8"); //告知浏览器响应的数据是什么， 告知浏览器使用什么字符集进行解码
        response.getWriter().write(jsonString);
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //  1.接受数据
        BufferedReader br = request.getReader();
        String params = br.readLine();// json字符串
        //  2.将json数据转换成java对象
        Brand brand = JSON.parseObject(params, Brand.class);// 转换成Brand对象
        //  3.调用方法
        brandService.add(brand);
        //  4.响应成功标识
        response.getWriter().write("success");
    }

    public void selectById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String sid = request.getParameter("id");
        System.out.println(sid);
        int id = Integer.parseInt(sid);
        Brand brand = brandService.selectById(id);
        String jsonString = JSON.toJSONString(brand);
        response.setContentType("text/json;charset=utf-8"); //告知浏览器响应的数据是什么， 告知浏览器使用什么字符集进行解码
        response.getWriter().write(jsonString);
    }

    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //  1.接受数据
        request.setCharacterEncoding("utf-8");
        BufferedReader br = request.getReader();
        String params = br.readLine();// json字符串
        //  2.将json数据转换成java对象
        Brand brand = JSON.parseObject(params, Brand.class);// 转换成Brand对象
        //  3.调用方法
        brandService.Update(brand);
        //  4.响应成功标识
        response.getWriter().write("success");
    }

    public void deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String sid = request.getParameter("id");
        int id = Integer.parseInt(sid);

        brandService.deleteById(id);

        response.getWriter().write("success");
    }

    public void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //  1.接受数据
        BufferedReader br = request.getReader();
        String params = br.readLine();

        //  2.将json数据转换成id数组
        int[] ids = JSON.parseObject(params, int[].class);

        //  3.调用service方法
        brandService.deleteByIds(ids);

        //  响应成功标识
        response.getWriter().write("success");
    }

    public void selectByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //  1.接受数据  url？currentPage=&?pageSize=
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");

        //  2.将String类型转换成int类型
        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        //  3.调用service方法
        PageBean<Brand> brandPageBean = brandService.selectByPage(currentPage, pageSize);

        //  4.将java对象转换成json数据  序列化
        String jsonString = JSON.toJSONString(brandPageBean);

        //  5.响应json数据到页面
        response.setContentType("text/json;charset=utf-8"); //告知浏览器响应的数据是什么， 告知浏览器使用什么字符集进行解码
        response.getWriter().write(jsonString);
    }

    public void selectByPageAndCondition(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //  1.接受数据  url？currentPage=&?pageSize=  brand
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");
        BufferedReader br = request.getReader();
        String param = br.readLine();// json数据条件对象

        //  2.将String类型转换成int类型 将json数据转换为java对象
        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);
        Brand brand = JSON.parseObject(param, Brand.class);

        //  3.调用service方法
        PageBean<Brand> brandPageBean = brandService.selectByPageAndCondition(currentPage,pageSize,brand);

        //  4.将java对象转换成json数据  序列化
        String jsonString = JSON.toJSONString(brandPageBean);

        //  5.响应json数据到页面
        response.setContentType("text/json;charset=utf-8"); //告知浏览器响应的数据是什么， 告知浏览器使用什么字符集进行解码
        response.getWriter().write(jsonString);
    }
}
